import type { ComputedRef, Ref } from 'vue'
import { defineEmits } from 'vue'

export default function useProgress(
  currentTime: Ref<number>,
  currentSong: ComputedRef<ISongItem | undefined>
) {
  let touchMove: boolean = false
  const emits = defineEmits<{
    (event: 'update:current-time', currentTime: number): void
  }>()
  const sliderValue = ref<number>(0)
  watch(currentTime, (newTime) => {
    if (touchMove) {
      return
    }
    sliderValue.value = (newTime / (currentSong.value?.dt || 1)) * 100
  })
  // 进度变化且结束拖动后触发
  function sliderChange(value: number) {
    touchMove = false
    emits('update:current-time', (value * (currentSong.value?.dt || 1)) / 100)
  }
  //开始拖动时触发
  function dragStart() {
    touchMove = true
  }

  return {
    sliderValue,
    sliderChange,
    dragStart
  }
}
